package com.dq.controller.api.merchant.in.template;

import com.dq.domain.org.Org;
import com.dq.domain.sys.user.user.BossUser;
import com.dq.domain.sys.user.user.OrgUser;
import com.dq.domain.sys.user.user.StaffUser;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.service.AllService;
import com.dq.shiro.MyToken;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Component;

/**
 * @author liuhui
 * @date 2019/11/22 0022 15:24:58
 */
@Component
public class LoginTemplate extends AllService {

    public OrgUser.Vo toLogin(OrgUser orgUser){
        String token = tokenService.createToken(orgUser);
        Subject subject = SecurityUtils.getSubject();
        AuthenticationToken token1 = new MyToken(token);
        subject.login(token1);
        OrgUser.Vo orgUserVo = (OrgUser.Vo)orgUser.toVO(false);
        orgUserVo.setToken(token);
        orgUserVo.setOrgVo((Org.Vo) orgUser.getSection().toVO(false));
        return orgUserVo;
    }

    public Org getOrg(){
       SysUser sysUser =  shiroService.getUser();
       return (Org) sysUser.getSection();
    }

    public StaffUser.Vo toLogin(StaffUser staffUser){
        String token = tokenService.createToken(staffUser);
        Subject subject = SecurityUtils.getSubject();
        AuthenticationToken token1 = new MyToken(token);
        subject.login(token1);
        StaffUser.Vo staffUserVo = (StaffUser.Vo)staffUser.toVO(false);
        staffUserVo.setToken(token);
        return staffUserVo;
    }


    public BossUser.LoginVo toLogin(BossUser bossUser){
        String token = tokenService.createToken(bossUser);
        Subject subject = SecurityUtils.getSubject();
        AuthenticationToken token1 = new MyToken(token);
        subject.login(token1);
        BossUser.LoginVo loginVo = bossUser.toLoginVo();
        loginVo.setToken(token);
        return loginVo;
    }

}
